﻿using Nancy;
using API_Service.BizLogic.BasicData;

namespace API_Service.Module
{
    public class BasicDataModule : BaseAPIModule
    {
        private SKUService skuService = new SKUService();
        private LineGroupService _lineGroupSrc = new LineGroupService();
        private KPNPrefixCfgService _kpnPrefix = new KPNPrefixCfgService();
        private GroupKPNCodeService _groupKPN = new GroupKPNCodeService();
        private RouteService _route = new RouteService();
        private SKUService _skuSrcs = new SKUService();

        public BasicDataModule() : base("/basicdata")
        {
            Get["/"] = _ => Response.AsJson("基础资料维护");

            #region line and group confing and process api
            Get["/line/test"] = _ =>
            {
                return Response.AsJson(_lineGroupSrc.GetLineBaseCfgInfo("TESTLINE"));
            };
            //获取线别名称
            Get["/line/namelist"] = _ =>
            {
                return Response.AsJson(_lineGroupSrc.GetLineNameList());
            };

            //获取线体信息或单一线体信息
            Get["/line/getline"] = _ =>
            {
                var line_name = Request.Query.line;
                var shift = Request.Query.shift;

                ExecutionResult<dynamic> result = _lineGroupSrc.GetLineBaseCfgInfo(line_name, shift);

                return Response.AsJson(result);
            };

            //获取站别别名称
            Get["/group/getlist"] = _ =>
            {
                string group =(string) Request.Query.group;
                string section = (string)Request.Query.section;

                return Response.AsJson(_lineGroupSrc.GetGroupConfigList(group,section));
            };

            Get["/group/getsection"] = _ =>
            {
                return Response.AsJson(_lineGroupSrc.GetSectionNameList());
            };
            //add group
            Post["/group/add"] = _ =>
             {
                 string model = Request.Form["MODEL"];
                 ExecutionResult<dynamic> result;
                 result = _lineGroupSrc.AddGroupCfgInfo(model);

                 return Response.AsJson(result);
             };
            //update group
            Post["/group/update"] = _ =>
            {
                string model = Request.Form["MODEL"];
                ExecutionResult<dynamic> result;
                result = _lineGroupSrc.UpdateGroupCfgInfo(model);

                return Response.AsJson(result);
            };
            //delete group
            Post["/group/delete/{group_code}"] = p =>
            {
                int group_code = (int)p.group_code;
                ExecutionResult<dynamic> result;
                result = _lineGroupSrc.DeleteGroupCfgInfo(group_code);

                return Response.AsJson(result);
            };

            //新增线体信息
            Post["/line/add"] = _ =>
            {
                string model = Request.Form["MODEL"];
                ExecutionResult<dynamic> result;
                result = _lineGroupSrc.AddLineBaseInfo(model);

                return Response.AsJson(result);
            };

            //更新线体信息
            Post["/line/update"] = _ =>
            {
                string model = Request.Form["MODEL"];
                ExecutionResult<dynamic> result = _lineGroupSrc.UpdateLineBaseInfo(model);

                return Response.AsJson(result);
            };

            //新增线体信息
            Post["/line/delete/{id}"] = p =>
            {
                int id = (int)p.id;

                return Response.AsJson(_lineGroupSrc.DeleteLineBaseInfo(id));
            };
            #endregion

            #region 料头维护API
            //获取料头信息
            Get[ "/prefix/getlist"] = p =>
            {
                ExecutionResult<dynamic> result;
                var kpnPrefix = Request.Query.kpn_prefix;
                var cust_code = Request.Query.cust_code;

                result = _kpnPrefix.QueryKPNPreFixCfgData(kpnPrefix, cust_code);
                return Response.AsJson(result);
            };
            //新增料头信息
            Post["/prefix/add"] = _ =>
            {
                string model = Request.Form["MODEL"];
                
                ExecutionResult<dynamic> result = _kpnPrefix.AddKPNPrefix(model);

                return Response.AsJson(result);
            };
            //更新料头信息
            Post["/prefix/update"] = _ =>
            {
                string model = Request.Form["MODEL"];
                
                ExecutionResult<dynamic> result = _kpnPrefix.UpdateKPNPrefix(model);

                return Response.AsJson(result);
            };
            //删除料头信息
            Post["/prefix/delete/{id}"] = p =>
            {
                int _id = (int)p.id;

                return Response.AsJson(_kpnPrefix.DeleteKPNPrefix(_id));
            };
            #endregion

            #region 站别捡料信息维护
            //获取料头信息
            Get["/kpncode/getlist"] = _ =>
             {
                 ExecutionResult<dynamic> result;
                 result = new ExecutionResult<dynamic>();

                 result = _groupKPN.GetKPNCodeList();

                 return Response.AsJson(result);
             };
            Get["/groupkpn/getlist"] = p =>
            {
                ExecutionResult<dynamic> result;
                var _group = Request.Query.group;
                var _kpnType = Request.Query.kpntype;

                result = _groupKPN.GetGroupKPNCfgList(_group, _kpnType);
                return Response.AsJson(result);
            };
            //新增料头信息
            Post["/groupkpn/add"] = _ =>
            {
                string model = Request.Form["MODEL"];

                ExecutionResult<dynamic> result = _groupKPN.AddGroupKPNCfg(model);

                return Response.AsJson(result);
            };
            //更新料头信息
            Post["/groupkpn/update"] = _ =>
            {
                string model = Request.Form["MODEL"];

                ExecutionResult<dynamic> result = _groupKPN.UpdateGroupKPNCfg(model);

                return Response.AsJson(result);
            };
            //删除料头信息
            Post["/groupkpn/delete/{id}"] = p =>
            {
                int _id = (int)p.id;

                return Response.AsJson(_groupKPN.DeleteGroupKPNCfg(_id));
            };
            #endregion

            #region 工站及流程相关
            Get["/group/getnamelist"] = _ =>
            {
                var _section = (string)Request.Query.section;
                var result = _route.GetGroupNameList(_section);

                return Response.AsJson(result);
            };
            #endregion

            #region SKU维护及更新API
            Get["/sku"] = _ => Response.AsJson("SKU资料维护");
            //获取SKU List
            Get["/sku/getlist"] = _ =>
            {
                var sku = Request.Query.sku;
                var model = Request.Query.model;
                var skuname = Request.Query.skuname;
                var is_valid = Request.Query.is_valid;

                ExecutionResult<dynamic> result;
                result = new ExecutionResult<dynamic>();

                result = _skuSrcs.GetSKUInfoList();

                return Response.AsJson(result);

            };
            //SKU增加
            Post["/sku/add"] = _ =>
            {
                var vModel = Request.Form["MODEL"];
                ExecutionResult<dynamic> result;
                result = new ExecutionResult<dynamic>();

                var model = Newtonsoft.Json.JsonConvert.DeserializeObject(vModel);

                result = _skuSrcs.AddSKUInfo(model);

                return Response.AsJson(result);
            };
            //SKU更新
            Post["/sku/update"] = _ =>
             {
                 var vModel = Request.Form["MODEL"];
                 ExecutionResult<dynamic> result;
                 result = new ExecutionResult<dynamic>();

                 var model = Newtonsoft.Json.JsonConvert.DeserializeObject(vModel);

                 result = _skuSrcs.UpdateSKUInfo(model);

                 return Response.AsJson(result);
             };
            //SKU删除
            Post["/sku/delete/{id}"] = p =>
            {
                int id = (int)p.id;
                ExecutionResult<dynamic> result;
                result = new ExecutionResult<dynamic>();

                result = _skuSrcs.DelSKUInfo(id);

                return Response.AsJson(result);
            };
            #endregion
        }

    }
}